/*一对一约课方法*/
function ajaxDel(id,strdate,payCurrency,changeTeacher,lxflag,bookedUrl){
    if($('[name=classWay]:checked').val()==null || $('[name=classWay]:checked').val()==""){
        //$('input:radio:first').tooltip('destroy');
        $('[name=classWay]:first').parent().tooltip({title : "请选择上课方式！" });
        $('[name=classWay]:first').parent().tooltip('show');
        return false;
    }
    // console.log($('[name="lesson.orderId"]:checked').val())
    if($('[name="lesson.orderId"]:checked').val()==null || $('[name=orderid]:checked').val()==""){
        //$('input:radio:first').tooltip('destroy');
        $('[name="lesson.orderId"]:first').parent().tooltip({title : "请选择学习的套餐！" });
        $('[name="lesson.orderId"]:first').parent().tooltip('show');
        return false;
    }
    $("#confirm").attr("disabled",true);
    $("#popover"+id).attr("disabled",true);
    $('.loadingdiv').css('display','block');
    let styleIdArray = [];
    $('input[name="styleId"]:checked').each(function(i){
        styleIdArray.push($(this).val());
    });
    let urlstr = bookedUrl + '/course/booking/'+$('.orderList input[type="radio"]:checked').val()+'/'+strdate+'/'+$('.classWay:checked').val()+'/'+$('#recorder').val()+'/100/'+changeTeacher+'/'+lxflag
    $.ajax({
        type:'post',
        url: urlstr,
        traditional: true,
        data: {
            'tokenString': $('#tokenString').val(), //'1CBD4D40685C6C6E3F7A00F7BD25C7BD',
            'recordId': $('#recordId').val(),
            'sexId': $('.sexList').val(),
            'levelId': $('.levelList').val(),
            'groupId': $('.groupList').val(),
            'chineseLevelId': $('.chineseList').val(),
            'listStyleId': styleIdArray
        },
        success: function (data) {
            // availableButton()
            console.log(data)
            if (data.status === 1) {
                $("#popover"+id).addClass("btn-danger")
                $("#popover"+id).removeClass("btn-success")
                // $("#popover"+id).attr("yesclick",$("#popover"+id).attr("onclick"))
                // $("#popover"+id).attr("noclick","popoverCancel("+id+","+data.data.lessonId+",'"+strdate+"')")
                // $("#popover"+id).attr("onclick","popoverCancel("+id+","+data.data.lessonId+",'"+strdate+"')")
                createMessgae(data.data.classTime,data.data.teacher,data.data.classWayName,data.data.packageName,data.data.materialName,id)
            } else {
                $(".loadingdiv").css("display","none");
                if (data.errorCode === '1022') {
                    $("#popover"+id).popover('destroy');
                    setTimeout(function () {
                        var dataContent='<div style="text-align: left;margin-bottom:15px;word-wrap:break-word;">';
                        dataContent+='<div style="text-align: center;margin-top:10px;"><button type="button" class="btn btn-default" id="cancel" onclick="$(\'#popover'+id+'\').popover(\'destroy\');window.getOrder();">取消</button>&nbsp;&nbsp;';
                        dataContent+='<button type="button" class="btn btn-danger" id="confirm" onclick="setTimeout(function(){popoverHtml('+id+',\''+strdate+'\','+changeTeacher+',1,bookedUrl);}, 300);">确认</button><div>';
                        dataContent+='</div>';
                        $("#popover"+id).popover({
                            html: true,
                            title: '<span style="font-weight:bold;">是否预约非连续老师？</span>',
                            content: dataContent,
                            placement: 'auto'
                        });
                        $("#popover"+id).popover('show');
                    }, 300)
                } else {
                    $("#popover"+id).popover('destroy');
                    alert(data.msg)
                }
            }
        },
        error: function (err) {
            $(".loadingdiv").css("display","none");
            availableButton()
        }
    })
}
function popoverHtml(id,strdate,changeTeacher,lxflag,bookedUrl) {
    $("#popover"+id).popover('destroy');
    $('.loadingdiv').css('display','block');
    $.ajax({
        type: 'post',
        url: bookedUrl + '/course/class/ways',
        data: {
            tokenString: $('#tokenString').val()
        },
        success: function (data) {
            console.log(data)
            setTimeout(function(){$('.loadingdiv').css('display','none');},300)
            if (data.status === 1) {
                createHtml(id,strdate,changeTeacher,data.data,lxflag,bookedUrl);
            } else if (data.status === null || data.status === 0) {
                alert("获取上课平台出错")
            } else if (data.status === -1) {
                alert("您还没有登录，请您重新登录！");
            } else {
                alert(data.status)
            }
        },
        error: function(xhr, type, exception) {
            setTimeout(function(){$('.loadingdiv').css('display','none');},300)
            // view("异常！");
            alert(xhr.responseText, "Failed");
            //alert("删除数据异常，请与管理员联系！");
        }
    });
}

function createHtml(id,strdate,changeTeacher,listWays,lxflag,bookedUrl){

    $("[role=popover]").popover('destroy');
    disabledButton();
    var dataContent='<div style="text-align: left;margin-bottom:10px;word-wrap:break-word;padding-bottom:5px;"><strong>上课方式：</strong></div>';
    dataContent+='<div style="text-align: left;margin-bottom:10px;word-wrap:break-word;border-bottom: 1px solid #eee;padding-bottom:10px;">';
    for(var i=0;i<listWays.length;i++){
        if (listWays.length === 1) {
            dataContent+='<lable ><input checked type="radio" name="classWay" class="classWay" value="'+listWays[i].id+'"/>&nbsp;'+listWays[i].name+'</lable>';
        } else {
            dataContent+='<lable ><input type="radio" name="classWay" class="classWay" value="'+listWays[i].id+'"/>&nbsp;'+listWays[i].name+'</lable>';
        }
        if(i!=listWays.length-1){
            dataContent+='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
        }
    }

    dataContent+='<hidden id="changeTeacher" name="changeTeacher" value="'+changeTeacher+'"/></div>';
    dataContent+='<div style="text-align: center;">';
    dataContent+='<button type="button" class="btn btn-default" id="cancel" onclick="window.getOrder();$(\'#popover'+id+'\').popover(\'destroy\');">取消</button>&nbsp;&nbsp;';
    dataContent+='<button type="button" class="btn btn-danger" id="confirm" onclick="ajaxDel('+id+',\''+strdate+'\',1,'+changeTeacher+','+lxflag+',bookedUrl)">确认</button><div>';

    setTimeout(function () {
        $("#popover"+id).attr("data-title","<span style=\"font-weight:bold;\">你确定预约本节课吗？</span>");
        $("#popover"+id).popover({
            html : true ,
            title: function() {
                return $(this).attr("data-title");
            },
            content: dataContent,
            placement: 'auto'
        });
        $("#popover"+id).popover("show");
    }, 300)
}

//取消课程调用ajax
function ajaxCancel(id,lessonId,bookedUrl){
    //var formParam = $("#form1").serialize();//序列化表格内容为字符串
    $('.loadingdiv').css('display','block')
    var urlstr = bookedUrl + '/course/cancel/'+lessonId+'/'+$('#recorder').val()+'/0'
    $("[rel=popover]").popover('destroy');
    $("#popover"+id).attr("disabled",true);
    $("#confirm").attr("disabled",true);
    $('.loadingdiv').css('display','block');
    $.ajax({
        type: 'post',
        url: urlstr,
        data: {"tokenString": $('#tokenString').val(),"recordId": $('#recordId').val()},
        success: function (data) {
            // availableButton()
            $('.loadingdiv').css('display','none')
            console.log(data)
            if (data.status === 1) {
                $("#popover"+id).addClass("btn-success");
                $("#popover"+id).removeClass("btn-danger");
                // $("#popover"+id).attr("onclick",$("#popover"+id).attr("yesclick"));
                $("#popover"+id).popover('destroy');
                window.getOrder();
            } else {
                if (data.errorCode === '1028') {
                    alert(data.msg)
                }
            }
        },
        error: function (err) {
            console.log(err)
            $('.loadingdiv').css('display','none')
        }
    })
}
//取消课程调用（一对一）
function popoverCancel(id,lessonId,bookedUrl) {
    $("[rel=popover]").popover('destroy');
    var dataContent='<div style="text-align: center;"><div style="text-align: center;padding-bottom:10px;">您确认取消课程吗？</div>';
    dataContent+='<button type="button" class="btn btn-default" id="cancel" onclick="window.getOrder();$(\'#popover'+id+'\').popover(\'destroy\');">取消</button>&nbsp;&nbsp;';
    dataContent+='<button type="button" class="btn btn-danger" id="confirm" onclick="ajaxCancel('+id+','+lessonId+',bookedUrl);">确认</button><div>';
    disabledButton();
    $("#popover"+id).popover({
        html: true,
        title: '<span style="font-weight:bold;">取消课程重要提示</span>',
        content: dataContent,
        placement: 'top'
    });
    $("#popover"+id).popover("show");
}
//预约一对多课程
function popoverBooked(id,strdate) {
    $("[rel=popover]").popover('destroy');
    var dataContent='<div style="text-align: center;">';
    dataContent+='<button type="button" class="btn btn-default" onclick="$(\'#popover'+id+'\').popover(\'destroy\');">取消</button>&nbsp;&nbsp;';
    dataContent+='<button type="button" class="btn btn-danger" onclick="ajaxOMBooked('+id+',\''+strdate+'\')">确认</button><div>';
    disabledButton();
    $("#popover"+id).popover({
        html : true ,
        title: '<span style="font-weight:bold;">您确定要预约本节课程吗？</span>',
        content: dataContent,
        placement: 'top'
    });
    $("#popover"+id).popover("show");
    var mycount=setInterval(function(){$("#popover"+id).popover('destroy'); clearTimeout(mycount)},10000);
}
//生成订课提示信息框
function createMessgae(releaseTime,teacherLogin,classWay,orderName,textbooksName,id){
    disabledButton()
    $("#popover"+id).popover('destroy');
    $("#popover"+id).attr("data-title","<span style=\"font-weight:bold;\">课程详情</span>");
    var dataContent='<div style="text-align: left;margin-bottom:15px;word-wrap:break-word;">';
    dataContent += "<div>上课时间:"+releaseTime+"</div>";
    dataContent += "<div>上课老师:"+teacherLogin+"</div>";
    dataContent += "<div>上课方式:"+classWay+"</div>";
    dataContent += "<div>套餐名称:"+orderName+"</div>";
    dataContent += "<div>教材名称:"+textbooksName+"</div>";
    dataContent+='<div style="text-align: center;margin-top:10px;"><button type="button" class="btn btn-danger isok" id="confirm">确认</button><div>';
    dataContent+='</div>';
//	$("#popover"+id).attr('data-html', true)
//	$("#popover"+id).attr('data-title', '<span style="font-weight:bold;">预约成功</span>')
//	$("#popover"+id).attr('data-container', dataContent)
//	$("#popover"+id).attr('data-placement', 'auto')
    setTimeout(function () {
        $("#popover"+id).popover({
            html: true,
            title: '<span style="font-weight:bold;">预约成功</span>',
            content: dataContent,
            placement: 'auto'
        });
        $("#popover"+id).popover('show');
        $(".loadingdiv").css("display","none");
        $(".isok").click(function(){
            window.getOrder();
            // $(this).unbind("click");
            // $("#popover"+id).popover('destroy');
            // availableButton()
        })
    }, 300)
}

function disabledButton(){
    $(".timeList .btn-danger").attr("disabled",true);
    $(".timeList .btn-success").attr("disabled",true);
}
function availableButton(){
    $(".btn-danger").attr("disabled",false);
    $(".btn-success").attr("disabled",false);
    $(".disabled").attr("disabled",true);
}
function zhugeBook(phone,ssc,cc,stuid,teacherid,bookType,releaseTime){
    //诸葛io统计
    var encryption = new Encryption(phone);
    var usphone = encryption.init();
    zhuge.identify(usphone);
    zhuge.track('CRM-约课', {
        "渠道":"CRM",
        "学员手机号":usphone,
        "SSC":ssc,
        "CC":cc,
        "学员ID":stuid,
        "老师ID":teacherid,
        "用户ID":stuid,
        "套餐类型":bookType,
        "约课时间":releaseTime
    });
}